package com.jh3502.vivo.function.reimnew;

import android.support.annotation.NonNull;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jh3502.vivo.base.BaseCallback;
import com.jh3502.vivo.base.BaseJson;
import com.jh3502.vivo.base.BasePresenter;
import com.jh3502.vivo.base.BaseView;
import com.jh3502.vivo.function.dialog.TravelNumChooseBean;
import com.jh3502.vivo.function.dialog.TravelPersonChooseBean;
import com.jh3502.vivo.utils.Utils;

import java.util.List;
import java.util.Map;

/**
 * @author harisucici
 * @date 2017/6/19
 */

public class ReimNewPresenter extends BasePresenter<ReimNewPresenter.ReimNewView> {
    ReimNewModel mReimNewModel;

    ReimNewView mReimNewView;


    public void setReimNewView(ReimNewView mReimNewView) {
        this.mReimNewView = mReimNewView;

    }

    public void findExpensePerson() {
        mReimNewView.showLoadingDialog();
        if (mReimNewModel == null) {
            mReimNewModel = new ReimNewModel(mReimNewView.getActContext());
        }

        mReimNewModel.findExpensePerson(new BaseCallback(mReimNewView) {
            @Override
            public void responseSuccess(@NonNull BaseJson baseJson) {
                mReimNewView.findExpensePersonSuccess(jsa2List2(baseJson.getObj().toString()));
            }
        });
    }

    public void findTravelForm(Map<String, Object> body) {
        mReimNewView.showLoadingDialog();
        if (mReimNewModel == null) {
            mReimNewModel = new ReimNewModel(mReimNewView.getActContext());
        }

        mReimNewModel.findTravelForm(body, new BaseCallback(mReimNewView) {
            @Override
            public void responseSuccess(@NonNull BaseJson baseJson) {
                mReimNewView.findTravelFormSuccess(jsa2List1(baseJson.getObj().toString()));
            }
        });
    }

    public void toFindaccount(Map<String, Object> body) {
        mReimNewView.showLoadingDialog();
        if (mReimNewModel == null) {
            mReimNewModel = new ReimNewModel(mReimNewView.getActContext());
        }

        mReimNewModel.findaccount(body, new BaseCallback(mReimNewView) {
            @Override
            public void responseSuccess(@NonNull BaseJson baseJson) {
                mReimNewView.findaccountSuccess(Utils.getInstance().jso2map(baseJson.getObj().toString()));
            }
        });
    }

    public void toSubmitExpense(Map<String, Object> body) {
        mReimNewView.showLoadingDialog();
        if (mReimNewModel == null) {
            mReimNewModel = new ReimNewModel(mReimNewView.getActContext());
        }

        mReimNewModel.submitExpense(body, new BaseCallback(mReimNewView) {
            @Override
            public void responseSuccess(@NonNull BaseJson baseJson) {
                mReimNewView.submitExpenseSuccess();
            }
        });
    }

    public void toFindHotelStandard(Map<String, Object> body) {
        mReimNewView.showLoadingDialog();
        if (mReimNewModel == null) {
            mReimNewModel = new ReimNewModel(mReimNewView.getActContext());
        }

        mReimNewModel.findHotelStandard(body, new BaseCallback(mReimNewView) {
            @Override
            public void responseSuccess(@NonNull BaseJson baseJson) {
                String standard = Utils.getInstance().jso2map(
                        baseJson.getObj().toString()).get("standard").toString().trim();
                mReimNewView.findHotelStandard(Double.parseDouble(standard));
            }
        });
    }

    public List<TravelNumChooseBean> jsa2List1(String s) {
        return new Gson().fromJson(s,
                new TypeToken<List<TravelNumChooseBean>>() {
                }.getType());
    }

    public List<TravelPersonChooseBean> jsa2List2(String s) {
        return new Gson().fromJson(s,
                new TypeToken<List<TravelPersonChooseBean>>() {
                }.getType());
    }


    @Override
    public void cancelCall() {

    }

    public interface ReimNewView extends BaseView {


        void findExpensePersonSuccess(List<TravelPersonChooseBean> travelPersonChooseBeans);

        void findTravelFormSuccess(List<TravelNumChooseBean> travelNumChooseBeans);

        void findaccountSuccess(Map<String, Object> map);

        void submitExpenseSuccess();

        void findHotelStandard(double v);
    }
}
